package com.ming.pullloadmorerecyclerview_lib;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class PullLoadMoreViewAdapter extends RecyclerView.Adapter {
    private int SpanCount;
    private boolean isFooterViewEnable = true;
    private boolean isMoreEnable = true;
    private View footerView = null;
    private RecyclerView.Adapter adapter;
    
    public PullLoadMoreViewAdapter(int spanCount, boolean isFooterViewEnable, boolean isMoreEnable, View footerView,RecyclerView.Adapter adapter) {
        SpanCount = spanCount;
        this.isFooterViewEnable = isFooterViewEnable;
        this.isMoreEnable = isMoreEnable;
        this.footerView = footerView;
        this.adapter = adapter;
    }

    private final int FOOTER = 0x009012453;

    private boolean isLoadMoreItem(int position) {
        return isFooterViewEnable && isMoreEnable && position == getItemCount() - 1;
    }


    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams && holder.getLayoutPosition() == getItemCount() - 1) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            // 如果是刷新、加载更多或头布局、脚布局独占一行，否则按照设置展示
            p.setFullSpan(true); // 设置独占一行
        }
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        adapter.onAttachedToRecyclerView(recyclerView);
        final RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager.SpanSizeLookup spanSizeLookup = ((GridLayoutManager) manager).getSpanSizeLookup();
            GridLayoutManager.SpanSizeLookup sizeLookup = new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int i) {
                    int type = getItemViewType(i);
                    if (type == FOOTER) {
                        return SpanCount;
                    }
                    return spanSizeLookup.getSpanSize(i);
                }
            };
            ((GridLayoutManager) manager).setSpanSizeLookup(sizeLookup);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isLoadMoreItem(position)) {
            return FOOTER;
        } else {
            return adapter.getItemViewType(position);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == FOOTER) {
            return new FooterViewHolder(footerView);
        }
        return adapter.onCreateViewHolder(viewGroup, i);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (isLoadMoreItem(i)) {
            return;
        }
        adapter.onBindViewHolder(viewHolder, i);
    }

    @Override
    public int getItemCount() {
        int count = adapter == null ? 0 : adapter.getItemCount();
        if (count == 0) {
            return 0;
        }
        return isMoreEnable && isFooterViewEnable ? count + 1 : count;
    }

    @Override
    public long getItemId(int position) {
        return adapter.getItemId(position);
    }

    @Override
    public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        adapter.registerAdapterDataObserver(observer);
    }

    @Override
    public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        if (adapter.hasObservers()) {
            adapter.unregisterAdapterDataObserver(observer);
        }
    }

    private class FooterViewHolder extends RecyclerView.ViewHolder {

        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
